3 use MediaWiki\Storage\BlobStoreFactory
;
4 use MediaWiki\Storage\MutableRevisionRecord
;
5 use MediaWiki\Storage\RevisionAccessException
;
6 use MediaWiki\Storage\RevisionRecord
;
7 use MediaWiki\Storage\RevisionStore
;
8 use MediaWiki\Storage\SlotRecord
;
9 use MediaWiki\Storage\SqlBlobStore
;
10 use Wikimedia\Rdbms\IDatabase
;
11 use Wikimedia\Rdbms\LoadBalancer
;
14 * Test cases in RevisionTest should not interact with the Database.
15 * For test cases that need Database interaction see RevisionDbTestBase.
17 class RevisionTest
extends MediaWikiTestCase
{
19 public function provideConstructFromArray() {
20 yield
'with text' => [
22 'text' => 'hello world.',
23 'content_model' => CONTENT_MODEL_JAVASCRIPT
26 yield
'with content' => [
28 'content' => new JavaScriptContent( 'hellow world.' )
31 // FIXME: test with and without user ID, and with a user object.
32 // We can't prepare that here though, since we don't yet have a dummy DB
36 * @param string $model
39 public function getMockTitle( $model = CONTENT_MODEL_WIKITEXT
) {
40 $mock = $this->getMockBuilder( Title
::class )
41 ->disableOriginalConstructor()
43 $mock->expects( $this->any() )
44 ->method( 'getNamespace' )
45 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
46 $mock->expects( $this->any() )
47 ->method( 'getPrefixedText' )
48 ->will( $this->returnValue( 'RevisionTest' ) );
49 $mock->expects( $this->any() )
50 ->method( 'getDBkey' )
51 ->will( $this->returnValue( 'RevisionTest' ) );
52 $mock->expects( $this->any() )
53 ->method( 'getArticleID' )
54 ->will( $this->returnValue( 23 ) );
55 $mock->expects( $this->any() )
56 ->method( 'getModel' )
57 ->will( $this->returnValue( $model ) );
63 * @dataProvider provideConstructFromArray
64 * @covers Revision::__construct
65 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
67 public function testConstructFromArray( $rowArray ) {
68 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
69 $this->assertNotNull( $rev->getContent(), 'no content object available' );
70 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
71 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
75 * @covers Revision::__construct
76 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
78 public function testConstructFromEmptyArray() {
79 $rev = new Revision( [], 0, $this->getMockTitle() );
80 $this->assertNull( $rev->getContent(), 'no content object should be available' );
84 * @covers Revision::__construct
85 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
87 public function testConstructFromArrayWithBadPageId() {
88 MediaWiki\
suppressWarnings();
89 $rev = new Revision( [ 'page' => 77777777 ] );
90 $this->assertSame( 77777777, $rev->getPage() );
91 MediaWiki\restoreWarnings
();
94 public function provideConstructFromArray_userSetAsExpected() {
95 yield
'no user defaults to wgUser' => [
97 'content' => new JavaScriptContent( 'hello world.' ),
102 yield
'user text and id' => [
104 'content' => new JavaScriptContent( 'hello world.' ),
105 'user_text' => 'SomeTextUserName',
112 yield
'user text only' => [
114 'content' => new JavaScriptContent( 'hello world.' ),
115 'user_text' => '111.111.111.111',
123 * @dataProvider provideConstructFromArray_userSetAsExpected
124 * @covers Revision::__construct
125 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
127 * @param array $rowArray
128 * @param mixed $expectedUserId null to expect the current wgUser ID
129 * @param mixed $expectedUserName null to expect the current wgUser name
131 public function testConstructFromArray_userSetAsExpected(
136 $testUser = $this->getTestUser()->getUser();
137 $this->setMwGlobals( 'wgUser', $testUser );
138 if ( $expectedUserId === null ) {
139 $expectedUserId = $testUser->getId();
141 if ( $expectedUserName === null ) {
142 $expectedUserName = $testUser->getName();
145 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
146 $this->assertEquals( $expectedUserId, $rev->getUser() );
147 $this->assertEquals( $expectedUserName, $rev->getUserText() );
150 public function provideConstructFromArrayThrowsExceptions() {
151 yield
'content and text_id both not empty' => [
153 'content' => new WikitextContent( 'GOAT' ),
154 'text_id' => 'someid',
156 new MWException( "Text already stored in external store (id someid), " .
157 "can't serialize content object" )
159 yield
'unknown user id and no user name' => [
161 'content' => new JavaScriptContent( 'hello world.' ),
164 new MWException( 'user_text not given, and unknown user ID 9989' )
166 yield
'with bad content object (class)' => [
167 [ 'content' => new stdClass() ],
168 new MWException( 'content field must contain a Content object.' )
170 yield
'with bad content object (string)' => [
171 [ 'content' => 'ImAGoat' ],
172 new MWException( 'content field must contain a Content object.' )
174 yield
'bad row format' => [
175 'imastring, not a row',
176 new InvalidArgumentException(
177 '$row must be a row object, an associative array, or a RevisionRecord'
183 * @dataProvider provideConstructFromArrayThrowsExceptions
184 * @covers Revision::__construct
185 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
187 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
188 $this->setExpectedException(
189 get_class( $expectedException ),
190 $expectedException->getMessage(),
191 $expectedException->getCode()
193 new Revision( $rowArray, 0, $this->getMockTitle() );
197 * @covers Revision::__construct
198 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
200 public function testConstructFromNothing() {
201 $this->setExpectedException(
202 InvalidArgumentException
::class
207 public function provideConstructFromRow() {
208 yield
'Full construction' => [
212 'rev_text_id' => '2',
213 'rev_timestamp' => '20171017114835',
214 'rev_user_text' => '127.0.0.1',
216 'rev_minor_edit' => '0',
217 'rev_deleted' => '0',
219 'rev_parent_id' => '1',
220 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
221 'rev_comment_text' => 'Goat Comment!',
222 'rev_comment_data' => null,
223 'rev_comment_cid' => null,
224 'rev_content_format' => 'GOATFORMAT',
225 'rev_content_model' => 'GOATMODEL',
227 function ( RevisionTest
$testCase, Revision
$rev ) {
228 $testCase->assertSame( 42, $rev->getId() );
229 $testCase->assertSame( 23, $rev->getPage() );
230 $testCase->assertSame( 2, $rev->getTextId() );
231 $testCase->assertSame( '20171017114835', $rev->getTimestamp() );
232 $testCase->assertSame( '127.0.0.1', $rev->getUserText() );
233 $testCase->assertSame( 0, $rev->getUser() );
234 $testCase->assertSame( false, $rev->isMinor() );
235 $testCase->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
236 $testCase->assertSame( 46, $rev->getSize() );
237 $testCase->assertSame( 1, $rev->getParentId() );
238 $testCase->assertSame( 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', $rev->getSha1() );
239 $testCase->assertSame( 'Goat Comment!', $rev->getComment() );
240 $testCase->assertSame( 'GOATFORMAT', $rev->getContentFormat() );
241 $testCase->assertSame( 'GOATMODEL', $rev->getContentModel() );
244 yield
'default field values' => [
248 'rev_text_id' => '2',
249 'rev_timestamp' => '20171017114835',
250 'rev_user_text' => '127.0.0.1',
252 'rev_minor_edit' => '0',
253 'rev_deleted' => '0',
254 'rev_comment_text' => 'Goat Comment!',
255 'rev_comment_data' => null,
256 'rev_comment_cid' => null,
258 function ( RevisionTest
$testCase, Revision
$rev ) {
259 // parent ID may be null
260 $testCase->assertSame( null, $rev->getParentId(), 'revision id' );
263 $testCase->assertSame( $rev->getTimestamp(), '20171017114835', 'timestamp' );
264 $testCase->assertSame( $rev->getUserText(), '127.0.0.1', 'user name' );
265 $testCase->assertSame( $rev->getUser(), 0, 'user id' );
266 $testCase->assertSame( $rev->getComment(), 'Goat Comment!' );
267 $testCase->assertSame( false, $rev->isMinor(), 'minor edit' );
268 $testCase->assertSame( 0, $rev->getVisibility(), 'visibility flags' );
271 $testCase->assertNotNull( $rev->getSize(), 'size' );
272 $testCase->assertNotNull( $rev->getSha1(), 'hash' );
274 // NOTE: model and format will be detected based on the namespace of the (mock) title
275 $testCase->assertSame( 'text/x-wiki', $rev->getContentFormat(), 'format' );
276 $testCase->assertSame( 'wikitext', $rev->getContentModel(), 'model' );
282 * @dataProvider provideConstructFromRow
283 * @covers Revision::__construct
284 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
286 public function testConstructFromRow( array $arrayData, $assertions ) {
287 $data = 'Hello goat.'; // needs to match model and format
289 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
290 ->disableOriginalConstructor()
293 $blobStore->method( 'getBlob' )
294 ->will( $this->returnValue( $data ) );
296 $blobStore->method( 'getTextIdFromAddress' )
297 ->will( $this->returnCallback(
298 function ( $address ) {
299 // Turn "tt:1234" into 12345.
300 // Note that this must be functional so we can test getTextId().
301 // Ideally, we'd un-mock getTextIdFromAddress and use its actual implementation.
302 $parts = explode( ':', $address );
303 return (int)array_pop( $parts );
307 // Note override internal service, so RevisionStore uses it as well.
308 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
310 $row = (object)$arrayData;
311 $rev = new Revision( $row, 0, $this->getMockTitle() );
312 $assertions( $this, $rev );
316 * @covers Revision::__construct
317 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
319 public function testConstructFromRowWithBadPageId() {
320 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
321 $this->overrideMwServices();
322 MediaWiki\
suppressWarnings();
323 $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
324 $this->assertSame( 77777777, $rev->getPage() );
325 MediaWiki\restoreWarnings
();
328 public function provideGetRevisionText() {
329 yield
'Generic test' => [
330 'This is a goat of revision text.',
333 'old_text' => 'This is a goat of revision text.',
338 public function provideGetId() {
350 * @dataProvider provideGetId
351 * @covers Revision::getId
353 public function testGetId( $rowArray, $expectedId ) {
354 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
355 $this->assertEquals( $expectedId, $rev->getId() );
358 public function provideSetId() {
359 yield
[ '123', 123 ];
364 * @dataProvider provideSetId
365 * @covers Revision::setId
367 public function testSetId( $input, $expected ) {
368 $rev = new Revision( [], 0, $this->getMockTitle() );
369 $rev->setId( $input );
370 $this->assertSame( $expected, $rev->getId() );
373 public function provideSetUserIdAndName() {
374 yield
[ '123', 123, 'GOaT' ];
375 yield
[ 456, 456, 'GOaT' ];
379 * @dataProvider provideSetUserIdAndName
380 * @covers Revision::setUserIdAndName
382 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
383 $rev = new Revision( [], 0, $this->getMockTitle() );
384 $rev->setUserIdAndName( $inputId, $name );
385 $this->assertSame( $expectedId, $rev->getUser( Revision
::RAW
) );
386 $this->assertEquals( $name, $rev->getUserText( Revision
::RAW
) );
389 public function provideGetTextId() {
391 yield
[ [ 'text_id' => '123' ], 123 ];
392 yield
[ [ 'text_id' => 456 ], 456 ];
396 * @dataProvider provideGetTextId
397 * @covers Revision::getTextId()
399 public function testGetTextId( $rowArray, $expected ) {
400 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
401 $this->assertSame( $expected, $rev->getTextId() );
404 public function provideGetParentId() {
406 yield
[ [ 'parent_id' => '123' ], 123 ];
407 yield
[ [ 'parent_id' => 456 ], 456 ];
411 * @dataProvider provideGetParentId
412 * @covers Revision::getParentId()
414 public function testGetParentId( $rowArray, $expected ) {
415 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
416 $this->assertSame( $expected, $rev->getParentId() );
420 * @covers Revision::getRevisionText
421 * @dataProvider provideGetRevisionText
423 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
426 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
429 public function provideGetRevisionTextWithZlibExtension() {
430 yield
'Generic gzip test' => [
431 'This is a small goat of revision text.',
433 'old_flags' => 'gzip',
434 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
440 * @covers Revision::getRevisionText
441 * @dataProvider provideGetRevisionTextWithZlibExtension
443 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
444 $this->checkPHPExtension( 'zlib' );
445 $this->testGetRevisionText( $expected, $rowData );
448 private function getWANObjectCache() {
449 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
453 * @return SqlBlobStore
455 private function getBlobStore() {
456 /** @var LoadBalancer $lb */
457 $lb = $this->getMockBuilder( LoadBalancer
::class )
458 ->disableOriginalConstructor()
461 $cache = $this->getWANObjectCache();
463 $blobStore = new SqlBlobStore( $lb, $cache );
467 private function mockBlobStoreFactory( $blobStore ) {
468 /** @var LoadBalancer $lb */
469 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
470 ->disableOriginalConstructor()
472 $factory->expects( $this->any() )
473 ->method( 'newBlobStore' )
474 ->willReturn( $blobStore );
475 $factory->expects( $this->any() )
476 ->method( 'newSqlBlobStore' )
477 ->willReturn( $blobStore );
482 * @return RevisionStore
484 private function getRevisionStore() {
485 /** @var LoadBalancer $lb */
486 $lb = $this->getMockBuilder( LoadBalancer
::class )
487 ->disableOriginalConstructor()
490 $cache = $this->getWANObjectCache();
492 $blobStore = new RevisionStore( $lb, $this->getBlobStore(), $cache );
496 public function provideGetRevisionTextWithLegacyEncoding() {
497 yield
'Utf8Native' => [
498 "Wiki est l'\xc3\xa9cole superieur !",
502 'old_flags' => 'utf-8',
503 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
506 yield
'Utf8Legacy' => [
507 "Wiki est l'\xc3\xa9cole superieur !",
512 'old_text' => "Wiki est l'\xe9cole superieur !",
518 * @covers Revision::getRevisionText
519 * @dataProvider provideGetRevisionTextWithLegacyEncoding
521 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
522 $blobStore = $this->getBlobStore();
523 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
524 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
526 $this->testGetRevisionText( $expected, $rowData );
529 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
532 * Do not set the external flag!
533 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
535 yield
'Utf8NativeGzip' => [
536 "Wiki est l'\xc3\xa9cole superieur !",
540 'old_flags' => 'gzip,utf-8',
541 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
544 yield
'Utf8LegacyGzip' => [
545 "Wiki est l'\xc3\xa9cole superieur !",
549 'old_flags' => 'gzip',
550 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
556 * @covers Revision::getRevisionText
557 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
559 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
560 $this->checkPHPExtension( 'zlib' );
562 $blobStore = $this->getBlobStore();
563 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
564 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
566 $this->testGetRevisionText( $expected, $rowData );
570 * @covers Revision::compressRevisionText
572 public function testCompressRevisionTextUtf8() {
574 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
575 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
576 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
577 "Flags should contain 'utf-8'" );
578 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
579 "Flags should not contain 'gzip'" );
580 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
581 $row->old_text
, "Direct check" );
582 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
583 Revision
::getRevisionText( $row ), "getRevisionText" );
587 * @covers Revision::compressRevisionText
589 public function testCompressRevisionTextUtf8Gzip() {
590 $this->checkPHPExtension( 'zlib' );
592 $blobStore = $this->getBlobStore();
593 $blobStore->setCompressBlobs( true );
594 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
597 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
598 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
599 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
600 "Flags should contain 'utf-8'" );
601 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
602 "Flags should contain 'gzip'" );
603 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
604 gzinflate( $row->old_text
), "Direct check" );
605 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
606 Revision
::getRevisionText( $row ), "getRevisionText" );
610 * @covers Revision::loadFromTitle
612 public function testLoadFromTitle() {
613 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
614 $this->overrideMwServices();
615 $title = $this->getMockTitle();
618 'rev_id=page_latest',
619 'page_namespace' => $title->getNamespace(),
620 'page_title' => $title->getDBkey()
625 'rev_page' => $title->getArticleID(),
626 'rev_text_id' => '2',
627 'rev_timestamp' => '20171017114835',
628 'rev_user_text' => '127.0.0.1',
630 'rev_minor_edit' => '0',
631 'rev_deleted' => '0',
633 'rev_parent_id' => '1',
634 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
635 'rev_comment_text' => 'Goat Comment!',
636 'rev_comment_data' => null,
637 'rev_comment_cid' => null,
638 'rev_content_format' => 'GOATFORMAT',
639 'rev_content_model' => 'GOATMODEL',
642 $db = $this->getMock( IDatabase
::class );
643 $db->expects( $this->any() )
644 ->method( 'getDomainId' )
645 ->will( $this->returnValue( wfWikiID() ) );
646 $db->expects( $this->once() )
647 ->method( 'selectRow' )
649 $this->equalTo( [ 'revision', 'page', 'user' ] ),
650 // We don't really care about the fields are they come from the selectField methods
651 $this->isType( 'array' ),
652 $this->equalTo( $conditions ),
654 $this->stringContains( 'fetchRevisionRowFromConds' ),
655 // We don't really care about the options here
656 $this->isType( 'array' ),
657 // We don't really care about the join conds are they come from the joinCond methods
658 $this->isType( 'array' )
660 ->willReturn( $row );
662 $revision = Revision
::loadFromTitle( $db, $title );
664 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
665 $this->assertEquals( $row->rev_id
, $revision->getId() );
666 $this->assertEquals( $row->rev_len
, $revision->getSize() );
667 $this->assertEquals( $row->rev_sha1
, $revision->getSha1() );
668 $this->assertEquals( $row->rev_parent_id
, $revision->getParentId() );
669 $this->assertEquals( $row->rev_timestamp
, $revision->getTimestamp() );
670 $this->assertEquals( $row->rev_comment_text
, $revision->getComment() );
671 $this->assertEquals( $row->rev_user_text
, $revision->getUserText() );
674 public function provideDecompressRevisionText() {
675 yield
'(no legacy encoding), false in false out' => [ false, false, [], false ];
676 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
677 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
678 yield
'(no legacy encoding), string in with gzip flag returns string' => [
679 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
680 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
682 yield
'(no legacy encoding), string in with object flag returns false' => [
683 // gzip string below generated with serialize( 'JOJO' )
684 false, "s:4:\"JOJO\";", [ 'object' ], false,
686 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
688 // Using a TitleValue object as it has a getText method (which is needed)
689 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
693 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
695 // Using a TitleValue object as it has a getText method (which is needed)
696 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
697 [ 'object', 'gzip' ],
700 yield
'(ISO-8859-1 encoding), string in string out' => [
702 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
706 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
708 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
712 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
714 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
718 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
720 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
721 [ 'gzip', 'object' ],
727 * @dataProvider provideDecompressRevisionText
728 * @covers Revision::decompressRevisionText
730 * @param bool $legacyEncoding
732 * @param array $flags
733 * @param mixed $expected
735 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
736 $blobStore = $this->getBlobStore();
737 if ( $legacyEncoding ) {
738 $blobStore->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
741 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
744 Revision
::decompressRevisionText( $text, $flags )
749 * @covers Revision::getRevisionText
751 public function testGetRevisionText_returnsFalseWhenNoTextField() {
752 $this->assertFalse( Revision
::getRevisionText( new stdClass() ) );
755 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
756 yield
'Just text' => [
757 (object)[ 'old_text' => 'SomeText' ],
761 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
762 yield
'gzip text' => [
764 'old_text' => "sttttr\002\022\000",
765 'old_flags' => 'gzip'
770 yield
'gzip text and different prefix' => [
772 'jojo_text' => "sttttr\002\022\000",
773 'jojo_flags' => 'gzip'
781 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
782 * @covers Revision::getRevisionText
784 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
789 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
792 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
793 yield
'Just some text' => [ 'someNonUrlText' ];
794 yield
'No second URL part' => [ 'someProtocol://' ];
798 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
799 * @covers Revision::getRevisionText
801 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
805 Revision
::getRevisionText(
808 'old_flags' => 'external',
815 * @covers Revision::getRevisionText
817 public function testGetRevisionText_external_noOldId() {
819 'ExternalStoreFactory',
820 new ExternalStoreFactory( [ 'ForTesting' ] )
824 Revision
::getRevisionText(
826 'old_text' => 'ForTesting://cluster1/12345',
827 'old_flags' => 'external,gzip',
834 * @covers Revision::getRevisionText
836 public function testGetRevisionText_external_oldId() {
837 $cache = $this->getWANObjectCache();
838 $this->setService( 'MainWANObjectCache', $cache );
841 'ExternalStoreFactory',
842 new ExternalStoreFactory( [ 'ForTesting' ] )
845 $lb = $this->getMockBuilder( LoadBalancer
::class )
846 ->disableOriginalConstructor()
849 $blobStore = new SqlBlobStore( $lb, $cache );
850 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
854 Revision
::getRevisionText(
856 'old_text' => 'ForTesting://cluster1/12345',
857 'old_flags' => 'external,gzip',
863 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', 'tt:7777' );
864 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
868 * @covers Revision::userJoinCond
870 public function testUserJoinCond() {
871 $this->hideDeprecated( 'Revision::userJoinCond' );
873 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
874 Revision
::userJoinCond()
879 * @covers Revision::pageJoinCond
881 public function testPageJoinCond() {
882 $this->hideDeprecated( 'Revision::pageJoinCond' );
884 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
885 Revision
::pageJoinCond()
889 public function provideSelectFields() {
904 'rev_comment_text' => 'rev_comment',
905 'rev_comment_data' => 'NULL',
906 'rev_comment_cid' => 'NULL',
907 'rev_content_format',
925 'rev_comment_text' => 'rev_comment',
926 'rev_comment_data' => 'NULL',
927 'rev_comment_cid' => 'NULL',
933 * @dataProvider provideSelectFields
934 * @covers Revision::selectFields
935 * @todo a true unit test would mock CommentStore
937 public function testSelectFields( $contentHandlerUseDB, $expected ) {
938 $this->hideDeprecated( 'Revision::selectFields' );
939 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
940 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
941 $this->assertEquals( $expected, Revision
::selectFields() );
944 public function provideSelectArchiveFields() {
961 'ar_comment_text' => 'ar_comment',
962 'ar_comment_data' => 'NULL',
963 'ar_comment_cid' => 'NULL',
984 'ar_comment_text' => 'ar_comment',
985 'ar_comment_data' => 'NULL',
986 'ar_comment_cid' => 'NULL',
992 * @dataProvider provideSelectArchiveFields
993 * @covers Revision::selectArchiveFields
994 * @todo a true unit test would mock CommentStore
996 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
997 $this->hideDeprecated( 'Revision::selectArchiveFields' );
998 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
999 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
1000 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
1004 * @covers Revision::selectTextFields
1006 public function testSelectTextFields() {
1007 $this->hideDeprecated( 'Revision::selectTextFields' );
1008 $this->assertEquals(
1013 Revision
::selectTextFields()
1018 * @covers Revision::selectPageFields
1020 public function testSelectPageFields() {
1021 $this->hideDeprecated( 'Revision::selectPageFields' );
1022 $this->assertEquals(
1031 Revision
::selectPageFields()
1036 * @covers Revision::selectUserFields
1038 public function testSelectUserFields() {
1039 $this->hideDeprecated( 'Revision::selectUserFields' );
1040 $this->assertEquals(
1044 Revision
::selectUserFields()
1048 public function provideGetArchiveQueryInfo() {
1049 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD' => [
1051 'wgContentHandlerUseDB' => false,
1052 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1055 'tables' => [ 'archive' ],
1072 'ar_comment_text' => 'ar_comment',
1073 'ar_comment_data' => 'NULL',
1074 'ar_comment_cid' => 'NULL',
1079 yield
'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD' => [
1081 'wgContentHandlerUseDB' => true,
1082 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1085 'tables' => [ 'archive' ],
1102 'ar_comment_text' => 'ar_comment',
1103 'ar_comment_data' => 'NULL',
1104 'ar_comment_cid' => 'NULL',
1105 'ar_content_format',
1111 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH' => [
1113 'wgContentHandlerUseDB' => false,
1114 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH
,
1119 'comment_ar_comment' => 'comment',
1137 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
1138 'ar_comment_data' => 'comment_ar_comment.comment_data',
1139 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1142 'comment_ar_comment' => [
1144 'comment_ar_comment.comment_id = ar_comment_id',
1149 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW' => [
1151 'wgContentHandlerUseDB' => false,
1152 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW
,
1157 'comment_ar_comment' => 'comment',
1175 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
1176 'ar_comment_data' => 'comment_ar_comment.comment_data',
1177 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1180 'comment_ar_comment' => [
1182 'comment_ar_comment.comment_id = ar_comment_id',
1187 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW' => [
1189 'wgContentHandlerUseDB' => false,
1190 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW
,
1195 'comment_ar_comment' => 'comment',
1213 'ar_comment_text' => 'comment_ar_comment.comment_text',
1214 'ar_comment_data' => 'comment_ar_comment.comment_data',
1215 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1218 'comment_ar_comment' => [
1220 'comment_ar_comment.comment_id = ar_comment_id',
1228 * @covers Revision::getArchiveQueryInfo
1229 * @dataProvider provideGetArchiveQueryInfo
1231 public function testGetArchiveQueryInfo( $globals, $expected ) {
1232 $this->setMwGlobals( $globals );
1234 $revisionStore = $this->getRevisionStore();
1235 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1236 $this->setService( 'RevisionStore', $revisionStore );
1238 $this->assertEquals(
1240 Revision
::getArchiveQueryInfo()
1244 public function provideGetQueryInfo() {
1245 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1247 'wgContentHandlerUseDB' => false,
1248 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1252 'tables' => [ 'revision' ],
1265 'rev_comment_text' => 'rev_comment',
1266 'rev_comment_data' => 'NULL',
1267 'rev_comment_cid' => 'NULL',
1272 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts page' => [
1274 'wgContentHandlerUseDB' => false,
1275 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1279 'tables' => [ 'revision', 'page' ],
1292 'rev_comment_text' => 'rev_comment',
1293 'rev_comment_data' => 'NULL',
1294 'rev_comment_cid' => 'NULL',
1305 [ 'page_id = rev_page' ],
1310 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts user' => [
1312 'wgContentHandlerUseDB' => false,
1313 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1317 'tables' => [ 'revision', 'user' ],
1330 'rev_comment_text' => 'rev_comment',
1331 'rev_comment_data' => 'NULL',
1332 'rev_comment_cid' => 'NULL',
1340 'user_id = rev_user',
1346 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts text' => [
1348 'wgContentHandlerUseDB' => false,
1349 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1353 'tables' => [ 'revision', 'text' ],
1366 'rev_comment_text' => 'rev_comment',
1367 'rev_comment_data' => 'NULL',
1368 'rev_comment_cid' => 'NULL',
1375 [ 'rev_text_id=old_id' ],
1380 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts 3' => [
1382 'wgContentHandlerUseDB' => false,
1383 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1385 [ 'text', 'page', 'user' ],
1387 'tables' => [ 'revision', 'page', 'user', 'text' ],
1400 'rev_comment_text' => 'rev_comment',
1401 'rev_comment_data' => 'NULL',
1402 'rev_comment_cid' => 'NULL',
1416 [ 'page_id = rev_page' ],
1422 'user_id = rev_user',
1427 [ 'rev_text_id=old_id' ],
1432 yield
'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1434 'wgContentHandlerUseDB' => true,
1435 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1439 'tables' => [ 'revision' ],
1452 'rev_comment_text' => 'rev_comment',
1453 'rev_comment_data' => 'NULL',
1454 'rev_comment_cid' => 'NULL',
1455 'rev_content_format',
1456 'rev_content_model',
1461 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH, opts none' => [
1463 'wgContentHandlerUseDB' => false,
1464 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH
,
1470 'temp_rev_comment' => 'revision_comment_temp',
1471 'comment_rev_comment' => 'comment',
1485 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1486 'rev_comment_data' => 'comment_rev_comment.comment_data',
1487 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1490 'temp_rev_comment' => [
1492 'temp_rev_comment.revcomment_rev = rev_id',
1494 'comment_rev_comment' => [
1496 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1501 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW, opts none' => [
1503 'wgContentHandlerUseDB' => false,
1504 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW
,
1510 'temp_rev_comment' => 'revision_comment_temp',
1511 'comment_rev_comment' => 'comment',
1525 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1526 'rev_comment_data' => 'comment_rev_comment.comment_data',
1527 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1530 'temp_rev_comment' => [
1532 'temp_rev_comment.revcomment_rev = rev_id',
1534 'comment_rev_comment' => [
1536 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1541 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW, opts none' => [
1543 'wgContentHandlerUseDB' => false,
1544 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW
,
1550 'temp_rev_comment' => 'revision_comment_temp',
1551 'comment_rev_comment' => 'comment',
1565 'rev_comment_text' => 'comment_rev_comment.comment_text',
1566 'rev_comment_data' => 'comment_rev_comment.comment_data',
1567 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1570 'temp_rev_comment' => [
1572 'temp_rev_comment.revcomment_rev = rev_id',
1574 'comment_rev_comment' => [
1576 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1584 * @covers Revision::getQueryInfo
1585 * @dataProvider provideGetQueryInfo
1587 public function testGetQueryInfo( $globals, $options, $expected ) {
1588 $this->setMwGlobals( $globals );
1590 $revisionStore = $this->getRevisionStore();
1591 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1592 $this->setService( 'RevisionStore', $revisionStore );
1594 $this->assertEquals(
1596 Revision
::getQueryInfo( $options )
1601 * @covers Revision::getSize
1603 public function testGetSize() {
1604 $title = $this->getMockTitle();
1606 $rec = new MutableRevisionRecord( $title );
1607 $rev = new Revision( $rec, 0, $title );
1609 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1611 $rec->setSize( 13 );
1612 $this->assertSame( 13, $rev->getSize() );
1616 * @covers Revision::getSize
1618 public function testGetSize_failure() {
1619 $title = $this->getMockTitle();
1621 $rec = $this->getMockBuilder( RevisionRecord
::class )
1622 ->disableOriginalConstructor()
1625 $rec->method( 'getSize' )
1626 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1628 $rev = new Revision( $rec, 0, $title );
1629 $this->assertNull( $rev->getSize() );
1633 * @covers Revision::getSha1
1635 public function testGetSha1() {
1636 $title = $this->getMockTitle();
1638 $rec = new MutableRevisionRecord( $title );
1639 $rev = new Revision( $rec, 0, $title );
1641 $emptyHash = SlotRecord
::base36Sha1( '' );
1642 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1644 $rec->setSha1( 'deadbeef' );
1645 $this->assertSame( 'deadbeef', $rev->getSha1() );
1649 * @covers Revision::getSha1
1651 public function testGetSha1_failure() {
1652 $title = $this->getMockTitle();
1654 $rec = $this->getMockBuilder( RevisionRecord
::class )
1655 ->disableOriginalConstructor()
1658 $rec->method( 'getSha1' )
1659 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1661 $rev = new Revision( $rec, 0, $title );
1662 $this->assertNull( $rev->getSha1() );
1666 * @covers Revision::getContent
1668 public function testGetContent() {
1669 $title = $this->getMockTitle();
1671 $rec = new MutableRevisionRecord( $title );
1672 $rev = new Revision( $rec, 0, $title );
1674 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1676 $content = new TextContent( 'Hello Kittens!' );
1677 $rec->setContent( 'main', $content );
1678 $this->assertSame( $content, $rev->getContent() );
1682 * @covers Revision::getContent
1684 public function testGetContent_failure() {
1685 $title = $this->getMockTitle();
1687 $rec = $this->getMockBuilder( RevisionRecord
::class )
1688 ->disableOriginalConstructor()
1691 $rec->method( 'getContent' )
1692 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1694 $rev = new Revision( $rec, 0, $title );
1695 $this->assertNull( $rev->getContent() );